#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //对象实例化
    mediaPlayer = new QMediaPlayer(ui->centralwidget);

    videoWiget = new QVideoWidget(ui->centralwidget);

    //设置播放画面的窗口
    videoWiget->setMinimumSize(800, 600);

    //实例化窗口布局---垂直布局
    QVBoxLayout* vbox = new QVBoxLayout(ui->centralwidget);
    ui->centralwidget->setLayout(vbox);

    //实例化选择视频按钮
    chooseButton = new QPushButton("选择视频",ui->centralwidget);

    //实例化播放按钮
    playButton = new QPushButton(ui->centralwidget);

    //设置图标代替文件
    playButton->setIcon(ui->centralwidget->style()->standardIcon(QStyle::SP_MediaPlay));

    //实例化一个水平布局，将以上控件放入水平布局中
    QHBoxLayout *hbox = new QHBoxLayout;

    hbox->addWidget(chooseButton);
    hbox->addWidget(playButton);

    //将播放窗口和水平布局都添加到垂直布局中
    vbox->addWidget(videoWiget);
    vbox->addLayout(hbox);

    //将选择视频对应的按钮和槽函数进行关联
    connect(chooseButton, &QPushButton::clicked, this, &MainWindow::chooseVideo);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::chooseVideo()
{
    //选择视频，返回一个播放视频的名字
    QString name = QFileDialog::getOpenFileName(this, "选择视频", ".", "WMV(*.wmv)");
    //设置媒体声音
    mediaPlayer->setMedia(QUrl(name));
    //输出视频画面
    mediaPlayer->setVideoOutput(videoWiget);
    //播放
    mediaPlayer->play();
}

